package com.wxb.hello.activemq.producer;


import com.wxb.hello.activemq.repository.MessageObject;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQBytesMessage;
import org.apache.activemq.command.ActiveMQStreamMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * describe:
 *
 * @author : 王校兵
 * @version : v1.0
 * @time : 2017/7/26  18:57
 */
//MessageProducer，是消息发送者,注册为一个bean
//@Component
//开启定时器
//@EnableScheduling
public class MessageProduction {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;//使用JmsMessagingTemplate将消息放入队列

//    @Autowired
//    private Queue queue;

    //@Scheduled(fixedDelay = 999999999)//每3s执行1次,将消息放入队列内
    public void sendQueue() throws JMSException {
//        this.jmsMessagingTemplate.convertAndSend(queue, "测试queue消息队列" + System.currentTimeMillis());

//        MessageObject messageObject = new MessageObject("admin", "123", "admin");

//        HashMap<String, String> paramMap = new HashMap<String, String>();
//        paramMap.put("a","a");
//        paramMap.put("b","b");
//        paramMap.put("c","c");

        //this.jmsMessagingTemplate.convertAndSend(queue, smsg);


//        byte[] content = getFileByte("e://ArtistAlbum.java");

//        BytesMessage message = new ActiveMQBytesMessage();
//        byte[] content = "demo".getBytes();
//        message.writeBytes(content);



//        StreamMessage message = new ActiveMQStreamMessage();
//        message.writeString("我是StreamMessage流消息");
//        jmsMessagingTemplate.convertAndSend(queue, message);
    }

    private byte[] getFileByte(String filename) {
        byte[] buffer = null;
        FileInputStream fin = null;
        try {
            File file = new File(filename);
            fin = new FileInputStream(file);
            buffer = new byte[fin.available()];
            fin.read(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buffer;
    }

}
